package com.wfk.plat.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfk.constant.Constant;
import com.wfk.entity.OpinionInfos;
import com.wfk.entity.SysMenus;
import com.wfk.entity.SysNews;
import com.wfk.entity.SysRole;
import com.wfk.entity.SysUser;
import com.wfk.enums.HandleType;
import com.wfk.plat.annotation.AuthResource;
import com.wfk.plat.mapping.MappingUtils;
import com.wfk.plat.service.ManagerService;
import com.wfk.service.IBaseInfoService;
import com.wfk.service.IMoneyDetailsService;
import com.wfk.service.IOpinionInfosService;
import com.wfk.service.IOrderGoodsService;
import com.wfk.service.ISysCashDetailsService;
import com.wfk.service.ISysNewsService;
import com.wfk.service.ISysRoleService;
import com.wfk.service.ISysUserService;
import com.wfk.utils.BasePage;
import com.wfk.utils.ResponseData;

import cn.hutool.core.date.DateUtil;

@Controller
public class PlatController {
	@Autowired
	private IBaseInfoService baseInfoService ;
	@Autowired
	private ISysUserService sysUserService ;
	@Autowired
	private ISysRoleService sysRoleService ;
	@Autowired
	private ManagerService managerService ;
	@Autowired
	private ISysNewsService sysNewsService;
	@Autowired
	private IOrderGoodsService orderGoodsService;
	@Autowired
	private ISysCashDetailsService sysCashDetailsService;
	@Autowired
	private IMoneyDetailsService moneyDetailsService;
	@Autowired
	private IOpinionInfosService opinionInfosService;
	/**
	 * 鏂伴椈椤甸潰
	 * @return
	 */
	@AuthResource
	@RequestMapping(value = MappingUtils.showSysNews)
	public ModelAndView showSysnews(Integer loginId,BasePage<SysNews> page) {
		sysNewsService.page(page,new QueryWrapper<SysNews>().orderByDesc("id"));
		ModelAndView view = new ModelAndView(MappingUtils.showSysNews);
		view.addObject("list", page);
		view.addObject("loginId", loginId);
		return view;
	}
	
	/**
	 * 鏂伴椈椤甸潰
	 * @return
	 */
	@AuthResource
	@RequestMapping(value = MappingUtils.opinionList)
	public ModelAndView opinionList(Integer loginId,BasePage<OpinionInfos> page) {
		opinionInfosService.getList(new OpinionInfos(),page);
		ModelAndView view = new ModelAndView(MappingUtils.opinionList);
		view.addObject("list", page);
		view.addObject("loginId", loginId);
		return view;
	}
	
	
	/**
	 * 淇濆瓨鏂伴椈淇℃伅
	 * @param nid
	 * @return
	 */
	@AuthResource(url=MappingUtils.showSysNews)
	@RequestMapping(value = MappingUtils.saveSysNews)
	@ResponseBody
	public ResponseData saveNews(SysNews sysNews) {
		ResponseData result = sysNewsService.saveNews(sysNews);
		return result;
	}
	
	/**
	 * 鍒犻櫎鏂伴椈
	 * @return
	 */
	@RequestMapping(value = MappingUtils.deleteSysNews)
	@ResponseBody
	public ResponseData deleteNews(Integer nid) {
		ResponseData result = sysNewsService.deleteNews(nid);
		return result;
	}
	
	/**
	 * 鏂板/缂栬緫  鏂伴椈椤甸潰
	 * @return
	 */
	@AuthResource(url=MappingUtils.showSysNews)
	@RequestMapping(value = MappingUtils.addSysNewsPage)
	public ModelAndView showAddNews(Long loginId,Integer id) {
		SysNews news = sysNewsService.getById(id);
		ModelAndView view = new ModelAndView(MappingUtils.addSysNewsPage);
		view.addObject("news", news);
		view.addObject("loginId",loginId);
		view.addObject("vo",news);
		return view;
	}
	
	
	@RequestMapping(value=MappingUtils.index)
	public ModelAndView index(Integer loginId) {
		String siteName = baseInfoService.getByKey(Constant.siteName);
		SysUser user = sysUserService.getById(loginId);
		SysRole role = sysRoleService.getById(user.getRoleid());
		List<SysMenus> menus = managerService.getMenus();
		ModelAndView m = new ModelAndView(MappingUtils.index);
		m.addObject("siteName", siteName) ;
		m.addObject("loginId", loginId) ;
		m.addObject("user", user) ;
		m.addObject("role", role) ;
		m.addObject("menus", menus) ;
		return m ;
	}
	
	@RequestMapping(value=MappingUtils.welcome)
	public ModelAndView welcome(Integer loginId) {
		SysUser user = sysUserService.getById(loginId);
		BigDecimal typeMoney = this.moneyDetailsService.typeMoney(HandleType.profit.getId(),DateUtil.yesterday());
		
		List<SysMenus> menus = managerService.getMenus();
		ModelAndView m = new ModelAndView(MappingUtils.welcome);
		m.addObject("user", user) ;
		m.addObject("menus", menus) ;
		m.addObject("loginId", loginId) ;
		m.addObject("orderMoney",orderGoodsService.moneyCount(1,null));
		m.addObject("cashWait",sysCashDetailsService.moneyCount(1, null));
		m.addObject("cashSuccess",sysCashDetailsService.moneyCount(1, null));
		m.addObject("cashReject",sysCashDetailsService.moneyCount(3, null));
		m.addObject("typeMoney",typeMoney);
		return m ;
	}
	
	@RequestMapping(value=MappingUtils.exit)
	public void exit(HttpServletResponse response,HttpServletRequest request,Integer loginId) throws IOException {
		Cookie cookie = new Cookie("token","");
		cookie.setDomain(request.getRemoteHost()) ;
		cookie.setPath("/");
		response.addCookie(cookie);
		
		response.sendRedirect(MappingUtils.login);
	}
}
